<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use Myredis\Myredis;
class Restpass extends Controller
{
    public function index()
    {
        return view('index');
    }

    /**处理修改密码
     * @param Request $request
     */
    public function doRest(Request $request)
    {
        if (!Request::instance()->isPost()){
            return $this->error('小伙子迷了路',url('index/index/index'));
        }
        //接受数据
        $tel = Request::instance()->post('phone');
        $telCode = Request::instance()->post('telCode');
        $pass = Request::instance()->post('pass');
        $newPass = Request::instance()->post('newPass');
        //验证数据
        $res = Db::table('xo_user')->field('pwd')->where('tel',$tel)->find();
        if (!$res){
            return $this->error('手机号码不存在');
        }
        if($telCode!=Myredis::get($tel.'telCode')){
            return $this->error('手机验证码不正确');
        }
        if(md5($pass)==$res['pwd']){
            return $this->error('和旧密码一致');
        }
        if($pass != $newPass){
            return $this->error('两次密码输入不正确');
        }
        // 处理数据
        $data = [
            'pwd'=>md5($pass)
        ];
        $res = Db::table('xo_user')->where('tel',$tel)->update($data);
        if($res){
            return $this->success('修改成功','index/login/index');
        }else {
            return $this->error('修改失败');
        }
    }
}
